package com.winshare.saleAssistant.controller;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import com.winshare.saleAssistant.config.WxMaConfiguration;
import com.winshare.saleAssistant.config.WxMaProperties;
import com.winshare.saleAssistant.domain.dto.req.WxGetOpenIdByCodeDTO;
import com.winshare.saleAssistant.domain.dto.resp.WxOpenIdAndTokenDTO;
import com.winshare.saleAssistant.domain.entity.StaffDO;
import com.winshare.saleAssistant.exception.BusinessException;
import com.winshare.saleAssistant.service.RedisService;
import com.winshare.saleAssistant.service.StaffService;
import com.winshare.saleAssistant.util.LogicResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import me.chanjar.weixin.common.error.WxErrorException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/sys/wx")
@CrossOrigin
@Api(value = "微信小程序授权相关",description = "微信小程序授权相关")
public class WxUserController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    private final WxMaProperties wxMaProperties;

    private final StaffService staffService;

    private final RedisService redisService;

    public WxUserController(WxMaProperties wxMaProperties, StaffService staffService, RedisService redisService) {
        this.wxMaProperties = wxMaProperties;
        this.staffService = staffService;
        this.redisService = redisService;
    }

    @PostMapping("/get/openId")
    @ApiOperation(value = "根据code获取openId,如果不是第一次使用其小程序，可作为登录接口",
            notes = "根据code获取openId，如果不是第一次使用其小程序，可作为小程序自动登录接口")
    public LogicResponse getOpenIdAndTokenByCode(@RequestBody @Validated WxGetOpenIdByCodeDTO wxGetOpenIdByCodeDTO) {
        final WxMaService wxService = WxMaConfiguration.getMaService(wxMaProperties.getConfigs().get(0).getAppid());
        try {
            WxMaJscode2SessionResult session = wxService.getUserService().getSessionInfo(wxGetOpenIdByCodeDTO.getCode());
            this.logger.info(session.getSessionKey());
            this.logger.info(session.getOpenid());
            String openId = session.getOpenid();
            StaffDO staffDOQuery = new StaffDO();
            staffDOQuery.setOpenId(openId);
            StaffDO staffDO = staffService.getStaffDetailByOpenId(staffDOQuery);
            WxOpenIdAndTokenDTO wxOpenIdAndTokenDTO = new WxOpenIdAndTokenDTO();
            if(null == staffDO) {
                wxOpenIdAndTokenDTO.setOpenId(openId);
                return LogicResponse.success(wxOpenIdAndTokenDTO);
            }
            wxOpenIdAndTokenDTO.setToken(staffDO.getSalt());
            redisService.setCurrentUserInfo(staffDO.getSalt(),staffDO,1296000);
            return LogicResponse.success(wxOpenIdAndTokenDTO);
        } catch (WxErrorException e) {
            this.logger.error(e.getMessage(), e);
            throw new BusinessException(e.getMessage());
        }
    }

}
